import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// 对话框底层UI
class BaseDialogWidget extends StatelessWidget {
  ///宽高
  final double? width;
  final double? height;
  final Decoration? decoration;
  final BorderRadiusGeometry? borderRadius;
  final EdgeInsetsGeometry? padding;
  final BoxConstraints? constraints;

  /// 标题
  final String title;
  final Widget? titleWidget;
  final double? titleSize;
  final FontWeight? titleWeight;

  /// 底部按钮
  final List<Widget>? actions;

  ///中间内容
  final Widget child;

  ///右上角关闭按钮点击，如果实现才会有关闭按钮
  final Function()? onCloseClick;

  ///外层安全区域
  final bool isSafeAreaTop;
  final bool isSafeAreaBottom;

  ///外层安全区域
  final Widget Function(Widget child)? safeAreaBuilder;

  //是否使用固有高度IntrinsicHeight,如果内容是List或者GradView或者RenderShrinkWrappingViewport ，请使用false
  final bool isIntrinsicHeight;

  //true:主体Column会加上MainAxisSize.min，并且去除Expanded。
  final bool shrinkWrap;

  const BaseDialogWidget({
    super.key,
    this.title = "",
    this.titleWidget,
    this.titleSize,
    this.titleWeight,
    this.width,
    this.height,
    this.decoration,
    this.constraints,
    this.borderRadius,
    this.padding,
    this.actions,
    this.isSafeAreaTop = false,
    this.isSafeAreaBottom = false,
    this.isIntrinsicHeight = true,
    this.shrinkWrap = false,
    this.safeAreaBuilder,
    required this.child,
    this.onCloseClick,
  });

  @override
  Widget build(BuildContext context) {
    final isShowTitle = titleWidget != null || title.isNotEmpty;
    final titlePadding = 10 + (onCloseClick == null ? 0.0 : 30.0);
    final shrinkWrapChild = shrinkWrap ? child : Expanded(child: child);
    //高度自适应
    Widget root = Stack(
      children: [
        Column(
          mainAxisSize: shrinkWrap ? MainAxisSize.min : MainAxisSize.max,
          children: <Widget>[
            //顶部标题
            if (isShowTitle) ...[
              Container(
                height: 45,
                padding: EdgeInsets.only(left: titlePadding, right: titlePadding),
                alignment: Alignment.center,
                child: titleWidget ?? Text(title, style: TextStyle(fontSize: titleSize ?? 18, fontWeight: titleWeight)),
              ),
              //间隔
              SizedBox(height: 5),
            ],
            //中间对话框内容
            shrinkWrapChild,
            //间隔
            SizedBox(height: 5),
            //底部按钮
            if (actions != null && actions!.isNotEmpty)
              Container(
                  height: 45,
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  child: Row(mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.center, children: actions!))
          ],
        ),
        if (onCloseClick != null)
          //关闭按钮
          Positioned(
            right: 0,
            child: IconButton(
              onPressed: onCloseClick,
              iconSize: 25,
              padding: const EdgeInsets.all(10),
              icon: const Icon(Icons.close),
            ),
          )
      ],
    );
    Widget myRoot = isIntrinsicHeight ? IntrinsicHeight(child: root) : root;
    if (safeAreaBuilder != null) {
      myRoot = safeAreaBuilder!(myRoot);
    } else {
      myRoot = isSafeAreaTop || isSafeAreaBottom ? SafeArea(top: isSafeAreaTop, bottom: isSafeAreaBottom, child: myRoot) : myRoot;
    }
    //高度自适应
    return Container(
      width: width,
      height: height,
      decoration: decoration ?? BoxDecoration(borderRadius: borderRadius ?? BorderRadius.circular(10), color: context.theme.dialogTheme.backgroundColor),
      constraints: constraints,
      padding: padding,
      //安全区域要保证背景不被覆盖
      child: myRoot,
    );
  }
}
